package com.frank.loadinglibrary.special.cool.letter;


import com.frank.loadinglibrary.special.cool.CoolLoadingComponent;
import com.frank.loadinglibrary.utils.SimpleAnimatorListener;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;

/**
 * 字母i的绘制以及动画
 * Created by yanxing on 16/2/18.
 */
public class ILetter extends Letter {
    public final static int STROKE_WIDTH = 20;
    public final static int WIDTH = STROKE_WIDTH;
    public final static int LENGTH = 120;
    private int mCurValue;
    private boolean isStart = false;
    private Paint mPaint;
    // 竖线弹出的时间
    private int mDuration1 = mDuration / 3 * 2;
    // 圆球弹出的时间
    private int mDuration2 = mDuration / 3;
    // 竖线变化的长度
    private float mLength1;
    // 圆球弹出的变化高度
    private float mLength2;
    // 圆球的半径
    private int mRadius;

    public ILetter(float x, float y) {
        super(x, y);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(CoolLoadingComponent.WHITE);
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setStrokeWidth(20);
        mCurY += LENGTH / 2;
    }

    @Override
    public void reset() {
        super.reset();
        if (mPaint != null) {
            mPaint.reset();
            mPaint.setAntiAlias(true);
            mPaint.setColor(CoolLoadingComponent.WHITE);
            mPaint.setStyle(Paint.Style.FILL_STYLE);
            mPaint.setStrokeWidth(20);

        }
        mCurY += LENGTH / 2;
        mDuration1 = mDuration / 3 * 2;
        mDuration2 = mDuration / 3;
        isStart = false;
        mLength1 = 0;
        mLength2 = 0;
        mRadius = 0;
    }

    @Override
    public void startAnim() {
        AnimatorValue animator = new AnimatorValue();
        animator.setDuration(mDuration1 + mDuration2);
        animator.setValueUpdateListener((animatorValue, v) -> {
            if (!isStart) {
                return;
            }
            mCurValue = (int) ((mDuration1 + mDuration2) * v);
            if (mCurValue <= mDuration1) {
                mLength1 = LENGTH * mCurValue / mDuration1;
            } else {
                mCurValue -= mDuration1;
                mRadius = 12 * mCurValue / 500;
                mLength2 = 30 * mCurValue / 500;
            }
        });
        animator.setStateChangedListener(new SimpleAnimatorListener() {
            @Override
            public void onStart(Animator animator) {
                super.onStart(animator);
                isStart = true;
                mRadius = 0;
            }

            @Override
            public void onEnd(Animator animator) {
                super.onEnd(animator);
                if (animationCallBack != null) {
                    animationCallBack.onLetterAnimatorEnd();
                }
            }
        });

        animator.start();
    }

    @Override
    public void drawSelf(Canvas canvas) {
        if (isStart) {
            // 绘制竖线
            canvas.drawLine(mCurX, mCurY, mCurX, mCurY - mLength1, mPaint);
            // 绘制圆点
            canvas.drawCircle(mCurX, mCurY - mLength1 + 30 - mLength2 - 20, mRadius, mPaint);
        }
    }
}
